package cn.com.ccxe.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.core.entity.Keyword;
import cn.com.ccxe.core.entity.MonitorType;
import cn.com.ccxe.core.entity.Website;
import cn.com.ccxe.dao.MonitorTypeDao;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.dao.impl.MonitorTypeDaoImpl;

public class TypeBussiness {

	private static final Logger 	logger = LoggerFactory.getLogger(TypeBussiness.class);
	private MonitorTypeDao 			dao = new MonitorTypeDaoImpl();

	public List<MonitorType> getAllMonitorType() {
		try {
			return dao.selectAll();
		} catch (Exception e) {
			logger.error("获取所有的监控类别出错！{}\n{}",e.getMessage(),e.getStackTrace());
		}
		return null;
	}
	
	public List<MonitorType> getAllByKeywordId(int keywordId) {
		try {
			return dao.getAllByKeywordId(keywordId);
		} catch (Exception e) {
			logger.error("获取所有的关键字出错！{}\n{}",e.getMessage(),e.getStackTrace());
		}
		return null;
	}

	/**
	 * 添加{@link MonitorType}
	 * @param monitorType
	 * @return
	 */
	public OperationStatus add(MonitorType monitorType) {
		return dao.add(monitorType);
	}

	/**
	 * 修改{@link MonitorType}
	 * @param monitorType
	 * @return
	 */
	public OperationStatus update(MonitorType monitorType) {
		if (monitorType.getId() > 0) {
			return dao.update(monitorType);
		}
		return OperationStatus.FAILURE;
	}

	/**
	 * 删除{@link MonitorType}，在进行删除操作时，必须确保任何{@link Website}、{@link Keyword}属于该类别。
	 * @param monitorType
	 * @return {@link OperationStatus}
	 */
	public OperationStatus del(MonitorType monitorType) {
		return del(monitorType.getId());
	}

	/**
	 * 根据{@link MonitorType}的唯一标识删除{@link MonitorType}
	 * @param monitorTypeId
	 * @return
	 */
	public OperationStatus del(int monitorTypeId) {
		if(!(monitorTypeId > 0)) {
			return OperationStatus.FAILURE;
		}
		if(dao.countRelationship(monitorTypeId) > 0) {
			return OperationStatus.RELATIONSHIP_EMPTY;
		}
		return dao.del(monitorTypeId);
	}

	public MonitorType get(int monitorTypeId) {
		if(!(monitorTypeId > 0)) {
			return null;
		}
		return dao.get(monitorTypeId);
	}

}
